package com.atguigu.gmall.cart.service.impl;

import com.atguigu.gmall.cart.mapper.CartInfoMapper;
import com.atguigu.gmall.cart.service.CartInfoAsyncService;
import com.atguigu.gmall.model.cart.CartInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @author wk
 * @create 2021-09-07 20:48
 * 使用异步多线程操作数据库
 */
@Service
@Slf4j
public class CartInfoAsyncServiceImpl implements CartInfoAsyncService {
    @Autowired
    private CartInfoMapper cartInfoMapper;

    // 异步插入数据库
    @Override
    @Async
    public void insertAsync(CartInfo cartInfo) {
        cartInfoMapper.insert(cartInfo);
        log.info("insertAsync子线程异步操作数据库" + Thread.currentThread().getName());
    }

    // 异步更新数据库
    @Async
    @Override
    public void updateAsync(CartInfo cartInfo) {
        cartInfoMapper.update(cartInfo, new QueryWrapper<CartInfo>()
                .eq("user_id", cartInfo.getUserId())
                .eq("sku_id", cartInfo.getSkuId()));
        log.info("updateAsync子线程异步操作数据库" + Thread.currentThread().getName());
    }
}
